package com.example.myys.viewModel.ysModel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.media3.common.util.UnstableApi
import com.example.myys.common.DataSource
import com.example.myys.common.VideoData
import com.example.myys.data.entity.DataBean
import com.example.myys.data.entity.ReadRecordBean
import com.example.myys.data.repository.MovieRepository
import com.example.myys.ui.Resource
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.util.Date


@UnstableApi
class VideoPlayViewModel() : ViewModel() {
    val movieRepository = MovieRepository
    //数据信息
    var videoDataState=DataBean()
    //历史信息
    var record = ReadRecordBean()
    //列表
    var videoListState: MutableList<DataSource> = mutableListOf()
    //播放位置
    var playIndex by mutableStateOf(0)

    //数据源位置
    var playSourceIndex by mutableStateOf(0)

    private val _videoState: MutableStateFlow<Resource<VideoData>> =
        MutableStateFlow(value = Resource.Loading())
    val videoState:StateFlow<Resource<VideoData>>
        get()=_videoState
    fun setDataBean(dataBean: String){
        if (dataBean.isNotEmpty()){
            val dataJsonBean= Gson().fromJson(dataBean, DataBean::class.java)
            videoDataState=dataJsonBean
            videoListState = dataJsonBean.dataSource.toMutableList()
            if (videoListState.isEmpty()) {
                viewModelScope.launch(Dispatchers.IO) {
                    videoDataState = movieRepository.getDetails(dataJsonBean)
                    videoListState = videoDataState.dataSource.toMutableList()
                    if (videoListState.isEmpty()){
                        _videoState.value=Resource.Error()
                    }else{
                        record = movieRepository.getRecord(dataJsonBean.id)
                        playIndex = record.chapterPos
                        getVideo(record.chapterPos)
                        if (record.id > 0) {
                            movieRepository.addMovieShelf(videoDataState)
                        }
                    }
                }
            }else{
                viewModelScope.launch(Dispatchers.IO) {
                    record = movieRepository.getRecord(dataJsonBean.id)
                    playIndex = record.chapterPos
                    getVideo(record.chapterPos)
                    if (record.id > 0) {
                        movieRepository.addMovieShelf(videoDataState)
                    }
                }
            }
        }
    }
    fun getVideo(index:Int){
        _videoState.value=Resource.Loading()
        val dataIndex = videoListState.get(playSourceIndex).episodes.get(index)
        viewModelScope.launch(Dispatchers.IO) {
            var url = movieRepository.getContent(dataIndex.url)
            url = url.replace("m3u.nikanba.live", "cos.m3u8hw8.com");
            _videoState.value = Resource.Success(
                VideoData(
                    index,
                    videoDataState.name,
                    dataIndex.name,
                    url,
                    videoListState
                )
            )
        }
        if (record.id > 0) {
            viewModelScope.launch(Dispatchers.IO) {
                record.chapterPos = index
                record.lastRead = Date().toString()
                movieRepository.saveRecord(record, true)
            }
        }
    }
    fun next(){
        if (playIndex + 1 < videoListState.get(playSourceIndex).episodes.size) {
            getVideo(playIndex+1)
            playIndex=playIndex+1
        }
    }
    fun previous(){
        if (playIndex - 1 < videoListState.get(playSourceIndex).episodes.size && playIndex - 1 >= 0) {
            getVideo(playIndex-1)
            playIndex=playIndex-1
        }
    }
    init {
    }
}